package pw.x3d.kotpress.core

import java.util.*

/**
 * Created by jimmy on 16/8/17.
 */
class Error {

    /**
     * Stores the list of errors.
     */
    protected var errors = mutableMapOf<String, String>();

    /**
     * Stores the list of data for error codes.
     */
    protected var error_data = mutableMapOf<String, String>();

    /**
     * Initialize the error.
     *
     * If `$code` is empty, the other parameters will be ignored.
     * When `$code` is not empty, `$message` will be used even if
     * it is empty. The `$data` parameter will be used only if it
     * is not empty.
     *
     * Though the class is constructed with a single error code and
     * message, multiple codes can be added using the `add()` method.
     *
     * @since 2.1.0
     *
     * @param String Error code
     * @param String Error message
     * @param String Optional. Error data.
     */
    constructor(code: String, message: String, data: String = "") {
        add(code, message, data)
    }

    /**
     * Retrieve all error codes.
     *
     * @return Set<String> List of error codes, if available.
     */
    fun get_error_codes(): Set<String> {
        return errors.keys;
    }

    /**
     * Retrieve first error code available.
     * @throws [NoSuchElementException]
     */
    fun get_error_code(): String {
        return get_error_codes().first();
    }

    /**
     * Retrieve all error messages or error messages matching code.
     *
     * @return array Error strings on success, or empty array on failure (if using code parameter).
     */
    fun get_error_messages(): Map<String, String> {
        return errors;
    }

    /**
     * Get single error message.
     *
     * This will get the first message available for the code. If no code is
     * given then the first code available will be used.
     *
     * @since 2.1.0
     *
     * @param string |int $code Optional. Error code to retrieve message.
     * @return String?
     */
    fun get_error_message(code: String): String? {
        return errors[code] ?: "";
    }

    /**
     * Retrieve error data for error code.
     *
     * @param String
     */
    fun get_error_data(code: String): String {

        return error_data[code] ?: "";
    }

    /**
     * Add an error or append additional message to an existing error.
     *
     * @param string|int $code Error code.
     * @param string $message Error message.
     * @param mixed $data Optional. Error data.
     */
    fun add(code: String, message: String, data: String = "") {
        add_message(code, message);

        add_data(code, data);
    }

    /**
     * Add an error or append additional message to an existing error.
     *
     * @param string|int $code Error code.
     * @param string $message Error message.
     * @param mixed $data Optional. Error data.
     */
    fun add_message(code: String, message: String) {
        errors[code] = message;
    }

    /**
     * Add data for error code.
     *
     * The error code can only contain one error data.
     */
    fun add_data(data: String, code: String = "") {
        error_data[code] = data;
    }

    /**
     * Removes the specified error.
     *
     * This function removes all error messages associated with the specified
     * error code, along with any error data for that code.
     */
    fun remove(code: String) {
        errors.remove(code);
        error_data.remove(code);
    }
}